package com.zjw.hyphenateui;

import android.app.Application;
import android.content.Context;
import android.text.TextUtils;

import com.example.havi.bean.HyChatUser;
import com.example.havi.helper.ChatHelper;
import com.example.havi.util.ChatSPUtil;
import com.tencent.bugly.crashreport.CrashReport;
import com.zjw.hyphenateui.helper.HyphenateLibraryCallBackHandle;
import com.zjw.hyphenateui.helper.UserHelper;

public class MainApplication extends Application {

    private static MainApplication instance;
    private static MyActivityLifecycle myActivityLifecycle;

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        myActivityLifecycle = new MyActivityLifecycle();
        registerActivityLifecycleCallbacks(myActivityLifecycle);
        CrashReport.initCrashReport(getApplicationContext(),"34edec6169", true);
        initHyphenateIM();
    }

    /**
     *
     * @return  true  处于后台   false  前台
     */
    public static boolean isAppBackground() {
        if (myActivityLifecycle.getStartCount() == 0) {
            return true;
        }
        return false;
    }

    private static ChatHelper chatHelper;
    //初始化环环信库
    private void initHyphenateIM() {
        //初始化
        chatHelper = ChatHelper.getInstance();
        chatHelper.init(this,new HyphenateLibraryCallBackHandle());

        String chat_user = ChatSPUtil.getString("chat_user","");
        String chat_pwd = ChatSPUtil.getString("chat_pwd","");
        //同步用户账号与环信账号信息 项目使用中应该判断是否登录
        if(!TextUtils.isEmpty(chat_user)){
            UserHelper.syncChatUserInfo(); //同步聊天用户信息
        }
    }

    public static ChatHelper getChatHelper(){
        return chatHelper;
    }

    public static Context getAppContext() {
        return instance.getApplicationContext();
    }
}
